

#include "gd32f30x_can.h"
#include "user_struct.h"
#include "can_msg_hdl.h"
#include "BMS_can_msg.h"
#include "fault_analyse.h"

STR_BMS_STATUS bmsStatus = {.fun_data_analyse = &BMS_data_analyse};

STR_SER_BMS_STATUS serBmsStatus = {.fsm1_data_analyse = &ser_BMS_fsm1_analyse, 
								   .fsm2_data_analyse = &ser_BMS_fsm2_analyse};

/*!
    \brief      
    \param[in]  none
    \param[out] none
    \retval     none
*/

void BMS_rece_canmailbox_set(uint8_t fifo_num)
{
	
	// -------------------- clean robot BMS canmailbox ------------------------
	
    can_filter_parameter_struct can_filter;
    can_struct_para_init(CAN_INIT_STRUCT, &can_filter);
	
	can_filter.filter_number      = 13;	
    can_filter.filter_mode        = CAN_FILTERMODE_LIST;
    can_filter.filter_bits        = CAN_FILTERBITS_32BIT;
    can_filter.filter_list_high   = ((ID_CAN_BMS_STATUS & 0xFFFFE000) >> 13);
    can_filter.filter_list_low    = (((ID_CAN_BMS_STATUS & 0x00001FFF) << 3) | (1 << 2));
    can_filter.filter_mask_high   = 0;
    can_filter.filter_mask_low    = 0;
    can_filter.filter_fifo_number = fifo_num;
    can_filter.filter_enable      = ENABLE;
    can_filter_init(&can_filter);	
	
	
	
	
	// ------------------- service robot BMS canmailbox ------------------------
	
	can_filter.filter_number      = 12;	
    can_filter.filter_mode        = CAN_FILTERMODE_LIST;
    can_filter.filter_bits        = CAN_FILTERBITS_16BIT;
    can_filter.filter_list_high   = (ID_CAN_SER_BMS_STATUS_X1 << 5);
    can_filter.filter_list_low    = (ID_CAN_SER_BMS_STATUS_X2 << 5);
    can_filter.filter_mask_high   = 0;
    can_filter.filter_mask_low    = 0;
    can_filter.filter_fifo_number = fifo_num;
    can_filter.filter_enable      = ENABLE;
    can_filter_init(&can_filter);
	
}


/*!
    \brief      
    \param[in]  none
    \param[out] none
    \retval     none
*/

void BMS_data_analyse(void)
{
	bmsStatus.rece_cnt++;
	
	bmsStatus.current_A = (bmsStatus.raw.bit.current - 5000) * 0.1f;
	bmsStatus.voltage_V = (bmsStatus.raw.bit.voltage) * 0.1f;
	
	reset_comm_loss_time(ENUM_BAT);
}



/*!
    \brief      
    \param[in]  none
    \param[out] none
    \retval     none
*/

void ser_BMS_fsm1_analyse(void)
{
	serBmsStatus.rece_cnt++;
	
	serBmsStatus.current_A = (serBmsStatus.FSM1.bit.current * 0.01f) - 327.68f;
	serBmsStatus.voltage_V = (serBmsStatus.FSM1.bit.volatage * 0.01f) - 327.68f;
	
	serBmsStatus.soc = serBmsStatus.FSM1.bit.soc * 0.1f;
	
	reset_comm_loss_time(ENUM_BAT);	
	
}



/*!
    \brief      
    \param[in]  none
    \param[out] none
    \retval     none
*/

void ser_BMS_fsm2_analyse(void)
{
	serBmsStatus.rece_cnt++;
	
	serBmsStatus.bat_temp = serBmsStatus.FSM2.bit.bat_temp - 40;
	serBmsStatus.health_rate = serBmsStatus.FSM2.bit.health_rate * 0.1f;
	
	reset_comm_loss_time(ENUM_BAT);	
}




